package com.mars.module.admin.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.mars.common.base.UserContextInfo;
import com.mars.common.response.PageInfo;
import com.mars.framework.context.ContextUserInfoThreadHolder;
import com.mars.framework.exception.ServiceException;
import com.mars.module.admin.entity.ApCookbook;
import com.mars.module.admin.mapper.ApCookbookMapper;
import com.mars.module.admin.service.IApCookbookService;
import com.mars.module.system.entity.BaseEntity;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import com.mars.module.admin.request.ApCollectRequest;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import com.mars.module.admin.mapper.ApCollectMapper;
import org.springframework.beans.BeanUtils;
import com.mars.module.admin.entity.ApCollect;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.mars.module.admin.service.IApCollectService;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

/**
 * 收藏业务层处理
 *
 * @author mars
 * @date 2024-02-28
 */
@Slf4j
@Service
@AllArgsConstructor
public class ApCollectServiceImpl implements IApCollectService {

    private final ApCollectMapper baseMapper;

    private final ApCookbookMapper apCookbookMapper;

    private final IApCookbookService apCookbookService;

    @Override
    @Transactional(rollbackFor = Exception.class)
    public ApCollect add(ApCollectRequest request) {
        ApCollect entity = ApCollect.builder().build();
        BeanUtils.copyProperties(request, entity);
        baseMapper.insert(entity);
        return entity;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean delete(Long id) {
        return baseMapper.deleteById(id) > 0;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean deleteBatch(List<Long> ids) {
        return baseMapper.deleteBatchIds(ids) > 0;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean update(ApCollectRequest request) {
        ApCollect entity = ApCollect.builder().build();
        BeanUtils.copyProperties(request, entity);
        return baseMapper.updateById(entity) > 0;
    }

    @Override
    public ApCollect getById(Long id) {
        return baseMapper.selectById(id);
    }

    @Override
    public PageInfo<ApCollect> pageList(ApCollectRequest request) {
        Page<ApCollect> page = new Page<>(request.getPageNo(), request.getPageSize());
        LambdaQueryWrapper<ApCollect> query = this.buildWrapper(request);
        IPage<ApCollect> pageRecord = baseMapper.selectPage(page, query);
        return PageInfo.build(pageRecord);
    }


    @Override
    public List<ApCollect> list(ApCollectRequest request) {
        LambdaQueryWrapper<ApCollect> query = this.buildWrapper(request);
        return baseMapper.selectList(query);
    }

    @Override
    public void collect(ApCollectRequest request) {
        ApCookbook apCookbook = apCookbookMapper.selectOne(Wrappers.lambdaQuery(ApCookbook.class).eq(ApCookbook::getFoodId, request.getFoodId()));
        if (Objects.isNull(apCookbook)) {
            throw new ServiceException("当前菜谱已下架");
        }
        UserContextInfo info = ContextUserInfoThreadHolder.get();
        Long userId = info.getId();
        if (request.getType() == 1) {
            // 收藏
            ApCollect apCollect = ApCollect.builder().foodId(request.getFoodId()).userId(userId).build();
            baseMapper.insert(apCollect);
        } else {
            // 取消收藏
            baseMapper.delete(Wrappers.lambdaQuery(ApCollect.class).eq(ApCollect::getUserId, userId).eq(ApCollect::getFoodId, request.getFoodId()));
        }
        apCookbookService.updateCollectNum(request.getType(), String.valueOf(request.getFoodId()));

    }

    @Override
    public List<ApCookbook> mineCollect(ApCollectRequest apBannerRequest) {
        UserContextInfo info = ContextUserInfoThreadHolder.get();
        Long userId = info.getId();
        List<ApCollect> collectList = baseMapper.selectList(Wrappers.lambdaQuery(ApCollect.class)
                .eq(ApCollect::getUserId, userId).orderByAsc(BaseEntity::getCreateTime));
        if (CollectionUtils.isEmpty(collectList)) {
            return new ArrayList<>();
        }
        List<Long> foodIds = collectList.stream().map(ApCollect::getFoodId).collect(Collectors.toList());
        return apCookbookMapper.selectList(Wrappers.lambdaQuery(ApCookbook.class).in(
                ApCookbook::getFoodId, foodIds
        ));
    }

    private LambdaQueryWrapper<ApCollect> buildWrapper(ApCollectRequest param) {
        LambdaQueryWrapper<ApCollect> query = new LambdaQueryWrapper<>();
        return query;
    }

}
